/*
 * output.c
 *
 *  Created on: 04/11/2012
 *      Author: julian
 */

#include <stdio.h>
#include <stdlib.h>

#include "packet_output.h"


void print_tbuf(struct msgbuf_tx tbuf) {
    uint8_t i=0;
    printf("\n tbuf:");
    printf("\nlen: %d, api: 0x%X,  ", tbuf.trama.length, tbuf.trama.api_id);
    printf(" addr16: %04X", tbuf.trama.addr_16);
    printf(" addr64: %#X", tbuf.trama.addr_64 << 32);
    printf("%X ", tbuf.trama.addr_64);
    printf("fid: %d", tbuf.trama.frameid);
    printf("\ndata: ");
    for (i = 0; i < tbuf.trama.length; ++i) {
        printf("%X ", tbuf.trama.data[i]);
    }
}

int packetOutput(void * packet, uint8_t api_type) {

struct msgbuf_tx tbuf;
uint8_t i = 0;

    if (packet == NULL) {
#ifdef OUTPUT_DEBUG
        fprintf(stderr, "PacketOutput:Apuntador no válido.");
#endif
        return -1;
    }

    switch (api_type) {
    case TX_REQUEST_16: {

        xbee_TxFrame16_t packet16 = *(xbee_TxFrame16_t*) packet;

        tbuf.trama.length = packet16.dataSize;
        tbuf.trama.addr_16 = packet16.destinationAddress;
        tbuf.trama.frameid = packet16.frameId;
        tbuf.trama.api_id = TX_REQUEST_16;
        for (i = 0; i < packet16.dataSize; i++) {
            tbuf.trama.data[i] = packet16.data[i];
        }
        tbuf.mtype = TX_REQUEST_16;
#ifdef OUTPUT_DEBUG
        print_tx_16(packet16);
        //print_tbuf(tbuf);
#endif
        if (xbeeSend(&tbuf) == -1) {
#ifdef OUTPUT_DEBUG
            perror("PacketOutput.xbeeSend:Error al enviar hacia la cola de salida");
#endif
            return -1;
        }
        free(packet16.data);
        break;
    }

    case TX_REQUEST_64: {

        xbee_TxFrame64_t packet64 = *(xbee_TxFrame64_t*) packet;

        tbuf.trama.length = packet64.dataSize;
        tbuf.trama.addr_64 = packet64.destinationAddress;
        tbuf.trama.frameid = packet64.frameId;
        tbuf.trama.api_id = TX_REQUEST_64;
        for (i = 0; i < packet64.dataSize; i++) {
            tbuf.trama.data[i] = packet64.data[i];
        }
        tbuf.mtype = MQ_TX_REQUEST_64;
#ifdef OUTPUT_DEBUG
        print_tx_64(packet64);
        //print_tbuf(tbuf);
#endif
        if (xbeeSend(&tbuf) == -1) {
#ifdef OUTPUT_DEBUG
            perror("PacketOutput.xbeeSend:Error al enviar hacia la cola de salida");
#endif
            return -1;
        }
        free(packet64.data);
        break;
    }

    case AT_COMMAND: {

        xbee_AtCommand_t command = *(xbee_AtCommand_t*) packet;

        tbuf.trama.addr_16 = 0;
        tbuf.trama.addr_64 = 0;
        tbuf.trama.frameid = command.frameId;
        tbuf.trama.api_id = AT_COMMAND;
        tbuf.trama.length = command.parameterSize + 2;
        tbuf.trama.data[0] = command.command[0];
        tbuf.trama.data[1] = command.command[1];
        for (i = 0; i < command.parameterSize; i++) {
            tbuf.trama.data[2 + i] = command.parameter[i];
        }
        tbuf.mtype = AT_COMMAND;
#ifdef OUTPUT_DEBUG
        print_AT_request(command);
        //print_tbuf(tbuf);
#endif
        if (xbeeSend(&tbuf) == -1) {
#ifdef OUTPUT_DEBUG
            perror("PacketOutput.xbeeSend:Error al enviar hacia la cola de salida");
#endif
            return -1;
        }
        break;
    }
    case AT_COMMAND_REMOTE_REQUEST: {

        xbee_AtRemote_t remote_command = *(xbee_AtRemote_t*) packet;

        tbuf.trama.addr_16 = remote_command.destinationAddress16;
        tbuf.trama.addr_64 = remote_command.destinationAddress64;
        tbuf.trama.frameid = remote_command.frameId;
        tbuf.trama.api_id = AT_COMMAND_REMOTE_REQUEST;
        tbuf.trama.length = remote_command.valueSize + 2;
        tbuf.trama.data[0] = remote_command.options;
        tbuf.trama.data[1] = remote_command.command[0];
        tbuf.trama.data[2] = remote_command.command[1];
        for (i = 0; i < remote_command.valueSize; i++) {
            tbuf.trama.data[3 + i] = remote_command.value[i];
        }
        tbuf.mtype = AT_COMMAND_REMOTE_REQUEST;
#ifdef OUTPUT_DEBUG
        print_AT_remote_request(remote_command);
        //print_tbuf(tbuf);
#endif
        if (xbeeSend(&tbuf) == -1) {
#ifdef OUTPUT_DEBUG
            perror("PacketOutput.xbeeSend:Error al enviar hacia la cola de salida");
#endif
            return -1;
        }

        break;
    }
    case AT_COMMAND_QUEUE: {

        xbee_AtCommand_t command = *(xbee_AtCommand_t*) packet;

        tbuf.trama.addr_16 = 0;
        tbuf.trama.addr_64 = 0;
        tbuf.trama.frameid = command.frameId;
        tbuf.trama.api_id = AT_COMMAND_QUEUE;
        tbuf.trama.length = command.parameterSize + 2;
        tbuf.trama.data[1] = command.command[0];
        tbuf.trama.data[2] = command.command[1];
        for (i = 0; i < command.parameterSize; i++) {
            tbuf.trama.data[2 + i] = command.parameter[i];
        }
        tbuf.mtype = AT_COMMAND_QUEUE;
#ifdef OUTPUT_DEBUG
        print_AT_request(command);
        //print_tbuf(tbuf);
#endif
        if (xbeeSend(&tbuf) == -1) {
#ifdef OUTPUT_DEBUG
            perror("PacketOutput.xbeeSend:Error al enviar hacia la cola de salida");
#endif
            return -1;
        }
        break;
    }
    default:
#ifdef OUTPUT_DEBUG
        fprintf(stderr,"PacketOutput.Tipo de trama no válido");
#endif
        return -1;
        break;
    }
    return 0;

}
